package goodbaby.dklt.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.classic.core.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.R;

/**
 * Created by Jayden on 2016/4/19 14:01.
 *
 * @version V1.0
 * @Title:
 * @Description: ${todo}
 */
public class HealthFragment extends BaseFragment {
    private TextView tvTitle;

    private ImageView ivDot1,ivDot2,ivDot3;

    private ViewPager pagerHealth;

    private List<HealthBaseFragment> fragments = new ArrayList<HealthBaseFragment>(3);

    private FragmentManager mFragmentManager;

    private HealthAdapter mAdapter;


    @Override
    public int getLayoutResId() {
        return R.layout.fragment_health;
    }

    @Override
    public void initView(View parentView) {
        super.initView(parentView);

        tvTitle = (TextView) parentView.findViewById(R.id.tv_common_title);
        tvTitle.setText("健康管理");

        ivDot1 = (ImageView) parentView.findViewById(R.id.iv_health_dot_1);
        ivDot1.setEnabled(false);
        ivDot2 = (ImageView) parentView.findViewById(R.id.iv_health_dot_2);
        ivDot3 = (ImageView) parentView.findViewById(R.id.iv_health_dot_3);
        pagerHealth = (ViewPager) parentView.findViewById(R.id.vp_health);

        //初始化viewpager
        mFragmentManager = getActivity().getSupportFragmentManager();
        StepNumberFragment stepNumberFragment = new StepNumberFragment();
        SleepFragment sleepFragment = new SleepFragment();
        BodyInfoFragment bodyInfoFragment = new BodyInfoFragment();
        fragments.add(stepNumberFragment);
        fragments.add(sleepFragment);
        fragments.add(bodyInfoFragment);

        mAdapter = new HealthAdapter(mFragmentManager,fragments);

        pagerHealth.setOffscreenPageLimit(5);//防止重复调用onCreateview（）

        pagerHealth.setAdapter(mAdapter);
        pagerHealth.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ivDot1.setEnabled(position != 0);
                ivDot2.setEnabled(position != 1);
                ivDot3.setEnabled(position != 2);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        pagerHealth.getCurrentItem();

    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    private class HealthAdapter extends FragmentPagerAdapter{
        List<HealthBaseFragment> list;

        public HealthAdapter(FragmentManager fm, List<HealthBaseFragment> list){
            super(fm);
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            for(int i =0 ; i< fragments.size();i++){
                fragments.get(i).resetLoadDataState();
                if(i == pagerHealth.getCurrentItem()){
                    fragments.get(i).toLoadData();
                }
            }

        }
    }
}
